package routers

import (
	"gin/cors"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

type DataWrap struct {
	Page int         `json:"page"`
	Code int         `json:"code"`
	Data []*DataItem `json:"data"`
}

type DataItem struct {
	Name  string `json:"name"`
	Num   int    `json:"num"`
	Price string `json:"price"`
}

func InitRouter(r *gin.Engine) {
	//是有session
	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("mysession", store))

	r.GET("/ping", func(c *gin.Context) {
		var dw = &DataWrap{Page: 1, Code: 0}
		dw.Data = append(dw.Data, &DataItem{"t1", 1, "1.2"}, &DataItem{"t2", 2, "3.3334445"})
		c.JSON(200, gin.H{"data": dw})
	})

	//后端接口分组
	gateway := r.Group("/admin/")
	{
		gateway.GET("Login", UserLogin)

		gateway.GET("LoginOut", func(c *gin.Context) {
			session := sessions.Default(c)
			session.Delete("user")
			session.Save()
			c.JSON(200, gin.H{"code": 0, "message": "退出登录"})
		})

		//需要验证登录
		gateway.Use(middleware.Auth())
		{
			gateway.GET("userList", UserList)
		}

	}
}
